package com.saturn.sldb;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.saturn.app.db.DymaticCondition;
import com.saturn.app.db.ITransaction;
import com.saturn.app.db.ListData;
import com.saturn.app.db.ORMapping;
import com.saturn.app.db.ResultORMapping;
import com.saturn.app.db.SimpleDaoTemplate;
import com.saturn.app.utils.DateUtils;
import com.saturn.auth.User;

public class Person {
	
	private String id;
	
	private String identify;
	private String type;
	
	private String createTime;
	private String creater;
	private String createrName;
	private String createrDepartment;
	
	private String state;
	
	private String name;
	private String gender;
	private String age;
	
	private String homeSum;
	private String ill;
	private String deformity;
	
	private static ORMapping<Person> mapping = new ResultORMapping<Person>();
	private static Map<String, String> stateMap = new HashMap<String, String>();
	
	static {
		stateMap.put("创建", "申报");
		stateMap.put("申报", "初审");
		stateMap.put("初审", "核对");
		stateMap.put("核对", "通过");
	}
	
	public static int add(Person vo) {
		//指定值对象类型(VOClass)。例子：User
		//指定插入表名称(tableName)。例子：如user表3个列，tableName=user(id, name, gender)
		//根据列的顺序获取值对象的属性值。例子：vo.getId(), vo.getName(), vo.getGender()
		return SimpleDaoTemplate.update(
				"INSERT INTO sldb_person(" +
				"identify, type, createTime, creater, createrName, " +
				"createrDepartment, state, name, gender, " +
				"age, homeSum, ill, deformity) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 
				vo.identify,
				vo.type,
				vo.createTime,
				vo.creater,
				vo.createrName,
				vo.createrDepartment,
				vo.state,
				vo.name,
				vo.gender,
				vo.age,
				vo.homeSum,
				vo.ill,
				vo.deformity
		);
	}
	
	public static int edit(Person vo) {
		//指定值对象类型(VOClass)。例子：User
		//指定插入表名称(tableName)。例子：如user表3个列，tableName=user
		//指定修改列信息(modify)。例子：name=?, value=?
		//根据修改列的顺序获取值对象的属性值。例子：vo.getName(), vo.getValue(), vo.getId()
		return SimpleDaoTemplate.update(
				"UPDATE sldb_person SET " +
				"identify = ?, type = ?, createTime = ?, creater = ?, createrName = ?, " +
				"createrDepartment = ?, state = ?, name = ?, gender = ?, " +
				"age = ?, homeSum = ?, ill = ?, deformity = ? " +
				"WHERE id = ?", 
				vo.identify,
				vo.type,
				vo.createTime,
				vo.creater,
				vo.createrName,
				vo.createrDepartment,
				vo.state,
				vo.name,
				vo.gender,
				vo.age,
				vo.homeSum,
				vo.ill,
				vo.deformity,
				vo.id
		);
	}
	
	public static Person get(String id) {
		//指定值对象类型(VOClass)。例子VOClass=User
		//指定表主键(key)。例子:key=id
		//指定插入表名称(tableName)。例子：如user表，tableName=user
		//指定O-R映射规则对象。默认mapping
		return SimpleDaoTemplate.queryOne(
				"SELECT * FROM sldb_person WHERE 1 = 1 and id = '" + id + "'",
				null, mapping, Person.class);
	}
	
	
	public static ListData<Person> getAll(Person vo, String start,
			String offset, String orderBy, String order) {
		//指定值对象类型(VOClass)。例子VOClass=User
		//指定插入表名称(tableName)。例子：如user表，tableName=user
		//指定O-R映射规则对象。默认mapping
		return SimpleDaoTemplate.query("SELECT * FROM sldb_person WHERE 1 = 1",
				new DymaticCondition().addSimpleCondition(vo, "state", "identify", "name", "gender", "createrName", "createTime")
						.addCondition("ORDER BY {0} {1}", orderBy, order),
				mapping, Person.class, start, offset);
	}
	
	public static int confirm(final String id, final String userId, final String note) {
		return SimpleDaoTemplate.update(new ITransaction() {
			public int execute(Connection connection) {
				
				User user = User.get(userId);
				Person person = Person.get(id);
				
				String nextState = stateMap.get(person.getState());
				
				PersonState personState = new PersonState(null, id, userId, user.getName(), DateUtils.getSystemTime(), person.getName(), person.getIdentify(), nextState, note);
				
				state(connection, id, nextState);
				PersonState.add(connection, personState);
				
				return 1;
			}
		});
	}
	
	public static int confirm(final String[] ids, final String userId, final String note) {
		for (String id : ids) {
			confirm(id, userId, note);
		}
		
		return 1;
	}
	
	public static int refuse(final String id, final String userId, final String note) {
		return SimpleDaoTemplate.update(new ITransaction() {
			public int execute(Connection connection) {
				
				User user = User.get(userId);
				Person person = Person.get(id);
				
				String nextState = "驳回";
				
				PersonState personState = new PersonState(null, id, userId, user.getName(), DateUtils.getSystemTime(), person.getName(), person.getIdentify(), nextState, note);
				
				state(connection, id, nextState);
				PersonState.add(connection, personState);
				
				return 1;
			}
		});
	}
	
	public static int refuse(final String[] ids, final String userId, final String note) {
		for (String id : ids) {
			refuse(id, userId, note);
		}
		
		return 1;
	}
	
	private static int state(Connection connection, String id, String state) {
		return SimpleDaoTemplate.update(connection, "UPDATE sldb_person SET state = ? WHERE id = ?",state, id);
	}
	
	public static int remove(final String id) {
		// 指定插入表名称(tableName)。例子：如user表，tableName=user
		return SimpleDaoTemplate.update(
				"DELETE FROM sldb_person WHERE id = ?", id);
	}

	public static void removes(String[] ids) {
		if (ids != null) {
			for (String id : ids) {
				remove(id);
			}
		}
	}
	
	public Person(String id, String identify, String type, String createTime, String creater,
			String createrName, String createrDepartment, String state,
			String name, String gender, String age,
			String homeSum, String ill, String deformity) {
		super();
		this.id = id;
		this.identify = identify;
		this.type = type;
		this.createTime = createTime;
		this.creater = creater;
		this.createrName = createrName;
		this.createrDepartment = createrDepartment;
		this.state = state;
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.homeSum = homeSum;
		this.ill = ill;
		this.deformity = deformity;
	}

	public Person() {
		super();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIdentify() {
		return identify;
	}

	public void setIdentify(String identify) {
		this.identify = identify;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getCreater() {
		return creater;
	}

	public void setCreater(String creater) {
		this.creater = creater;
	}

	public String getCreaterName() {
		return createrName;
	}

	public void setCreaterName(String createrName) {
		this.createrName = createrName;
	}

	public String getCreaterDepartment() {
		return createrDepartment;
	}

	public void setCreaterDepartment(String createDepartment) {
		this.createrDepartment = createDepartment;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getHomeSum() {
		return homeSum;
	}

	public void setHomeSum(String homeSum) {
		this.homeSum = homeSum;
	}

	public String getIll() {
		return ill;
	}

	public void setIll(String ill) {
		this.ill = ill;
	}

	public String getDeformity() {
		return deformity;
	}

	public void setDeformity(String deformity) {
		this.deformity = deformity;
	}
	
	@Override
	public String toString() {
		return new JSONObject(this).toString();
	}
}
